package com.zhongxi.guoyuan.adapter.personCenter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RatingBar;
import android.widget.TextView;

import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.clearweed.ClearEvaluateResult;

import org.json.JSONObject;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/1/11.
 */

public class ClearEvaluateAdapter extends BaseAdapter {

    private Context context;
    private List<ClearEvaluateResult.Comments> commentsList;
    private JSONObject user;

    public ClearEvaluateAdapter(Context context, List<ClearEvaluateResult.Comments> commentsList, JSONObject user) {
        this.context = context;
        this.commentsList = commentsList;
        this.user=user;
    }

    @Override
    public int getCount() {
        return commentsList.size() > 0 ? commentsList.size() : 0;
    }

    @Override
    public Object getItem(int i) {
        return commentsList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder=null;
        if (view==null){
            view = LayoutInflater.from(context).inflate(R.layout.clearweed_evaluate_layout, null);
            holder=new ViewHolder(view);
            
            view.setTag(holder);
        }else {
            holder = (ViewHolder) view.getTag();
        }
        if(null!=commentsList&&!"".equals(commentsList)){
            ClearEvaluateResult.Comments comments = commentsList.get(i);
            
            if(null!=comments.getContent()&&!"".equals(comments.getContent())){
                holder.clearWeedEvaluateContent.setText(comments.getContent());
            }else{
                holder.clearWeedEvaluateContent.setText("");
            }
            if(null!=comments.getCtime()&&!"".equals(comments.getCtime())){
                holder.clearWeedEvaluateTime.setText(comments.getCtime());
            }else{
                holder.clearWeedEvaluateTime.setText("");
            }
            if(null!=comments.getStarlevel()&&!"".equals(comments.getStarlevel())){
                holder.clearWeedEvaluateRatingBar.setRating(Float.parseFloat(comments.getStarlevel()));
            }else{
                holder.clearWeedEvaluateRatingBar.setRating(0);
            }
            if(null!=user&&!"".equals(user)){
             if(null!=comments.getUserid()&&!"".equals(comments.getUserid())){
                holder.clearWeedEvaluateName.setText(user.optString(comments.getUserid()));
             }else{
                 holder.clearWeedEvaluateName.setText("");
             }
            }else {
                holder.clearWeedEvaluateName.setText("");
            }
        }
        return view;
    }

    static class ViewHolder {
        @Bind(R.id.clearWeed_evaluate_content)
        TextView clearWeedEvaluateContent;
        @Bind(R.id.clearWeed_evaluate_ratingBar)
        RatingBar clearWeedEvaluateRatingBar;
        @Bind(R.id.clearWeed_evaluate_time)
        TextView clearWeedEvaluateTime;
        @Bind(R.id.clearWeed_evaluate_name)
        TextView clearWeedEvaluateName;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
